"""
    练习： 从列表[4,5,63,566,7,8,9,10]选出所有偶数
         结果存入另外的列表中，使用生成器实现
"""
ls = [4, 5, 63, 566, 7, 8, 9, 10]


class LsitIterator:
    """迭代器"""
    def __init__(self, ls):
        self.ls = ls
        self.index = 0

    def __next__(self):
        if self.index > len(self.ls) - 1:
            raise StopIteration
        so = self.ls[self.index]
        self.index += 1
        return so


class Listyield:
    def __init__(self, lis):
        self.lis = lis

    def yields(self):
        """yield生成器实现对象可迭代"""
        num = 0
        while num < len(self.lis):
            yield self.lis[num]
            num += 1

    def __iter__(self):
        """迭代器方法实现对象可迭代"""
        return LsitIterator(self.lis)


if __name__ == "__main__":
    # yield生成器实现对象可迭代
    op = Listyield(ls)
    ops = []
    for i in op.yields():
        if i % 2 == 0:
            ops.append(i)
    print(ops)

    # 迭代器方法实现对象可迭代
    opc = []
    for c in op:
        if c % 2 == 0:
            opc.append(c)
    print(opc)
